package org.bite.book_controller.service;

import org.bite.book_controller.Mapper.BookMapper;
import org.bite.book_controller.enums.StatusEnum;
import org.bite.book_controller.model.BookInfo;
import org.bite.book_controller.dao.BookDao;
import org.bite.book_controller.model.PageResult;
import org.bite.book_controller.model.RequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookMapper bookMapper;

    public List<BookInfo> getList(){
//        BookDao bookDao = new BookDao();
        List<BookInfo> bookInfos =bookDao.mockData();
        for(BookInfo bookInfo : bookInfos){
            if(bookInfo.getStatus() == 2){
                bookInfo.setStateCN("可借阅");
            }else{
                bookInfo.setStateCN("不可借阅");
            }
        }
        return bookInfos;
    }


    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListPage(RequestPage requestPage) {
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.getListByPage(requestPage.getOffset(), requestPage.getPageSize());
        //修改状态
        for(BookInfo bookInfo : bookInfos){
            bookInfo.setStateCN(StatusEnum.getNameByCode(bookInfo.getStatus()).getName());
        }

        return new PageResult<>(bookInfos,count,requestPage);
    }

    public BookInfo getBookInfo(Integer bookId) {
        return bookMapper.getBookInfo(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }
}
